# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

default_platform(:ios)

platform :ios do
  desc "Refresh dSYMs"
  lane :refresh_dsyms do
    upload_symbols_to_crashlytics(
      dsym_path: './Rainbow.app.dSYM.zip'
    )
    sentry_upload_dsym(
      dsym_path: './Rainbow.app.dSYM.zip'
    )

    version_from_plist = get_version_number(target: "Rainbow")

    release_version = "me.rainbow-#{version_from_plist}"

    dist = (latest_testflight_build_number(version: get_version_number(target: "Rainbow")) + 1).to_s

    sentry_create_release(
      finalize: true, # Whether to finalize the release. If not provided or false, the release can be finalized using the sentry_finalize_release action
      version: release_version
    )
    sentry_upload_sourcemap(
      sourcemap: './main.jsbundle', # file to upload
      rewrite: true,
      version: release_version,
      dist: dist
    )
    sentry_upload_sourcemap(
      sourcemap: './main.jsbundle.map', # sourcemap to upload
      rewrite: true,
      version: release_version,
      dist: dist
    )
    clean_build_artifacts
  end

  desc "Submit a new Beta build to Apple TestFlight"
  lane :beta do
    increment_build_number({
      build_number: latest_testflight_build_number(version: get_version_number(target: "Rainbow")) + 1
    })

    update_info_plist(
      plist_path: "Rainbow/Info.plist",
      display_name: "Rainbow"
    )
    update_app_identifier(
      app_identifier: "me.rainbow",
      xcodeproj: "Rainbow.xcodeproj",
      plist_path: "Rainbow/Info.plist",
    )
    match(
      force: true,
      type: "appstore",
      app_identifier: "me.rainbow",
      git_url: "git@github.com:rainbow-me/rainbow-code-signing.git"
    )
    automatic_code_signing(
      use_automatic_signing: false
    )
    gym(
      workspace: "Rainbow.xcworkspace",
      scheme: "Rainbow",
      include_symbols: true,
      configuration: "Release",
      export_method: "app-store",
    )

    # api_key = app_store_connect_api_key(
    #   key_id: "B6RRSJG6H6",
    #   issuer_id: "95830311-9e99-47b0-95ac-d628a3e4215e",
    #   key_filepath: "fastlane/AuthKey_B6RRSJG6H6.p8",
    #   duration: 1200, # optional
    #   in_house: false, # optional but may be required if using match/sigh
    # )

    pilot(
      app_identifier: "me.rainbow",
      skip_waiting_for_build_processing: true,
      # api_key: api_key
    )
  end
end
